package com.bci.pwtz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyMenuMapper;
import com.bci.pwtz.mysql.model.CompanyMenu;
import com.bci.pwtz.service.CompanyMenuService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class CompanyMenuServiceImpl implements CompanyMenuService
{

    private CompanyMenuMapper companyMenuMapper;
    
    /**
     * @return the companyMenuMapper
     */
    public CompanyMenuMapper getCompanyMenuMapper()
    {
        return companyMenuMapper;
    }

    /**
     * @param companyMenuMapper the companyMenuMapper to set
     */
    public void setCompanyMenuMapper(CompanyMenuMapper companyMenuMapper)
    {
        this.companyMenuMapper = companyMenuMapper;
    }

    private static final Logger _logger = Logger.getLogger(CompanyMenuServiceImpl.class);
    
    @Override
    public CompanyMenu add(CompanyMenu companyMenu) throws PwtzDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:companyMenu=" + companyMenu.toString());
        }
        if (companyMenu == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,companyMenu is null");
            }
            return null;
        }
        try
        {
            companyMenuMapper.insertSelective(companyMenu);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return companyMenu;
    }

    public PaginationList<CompanyMenu> findMenuInfo(int pid, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<CompanyMenu> paginationList = new PaginationList<CompanyMenu>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findMenuInfo:|pid=" + pid);
        }
        int totalCount = 0;
        try
        {
            totalCount = companyMenuMapper.selectMenuByPidNum(pid);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        List<CompanyMenu> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
                list = companyMenuMapper.selectMenuByPid(pid, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("loadMenuInfo:result menu=" + list.toString());
        }
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }

    public List<CompanyMenu> findMenuByPidOrderByRank(int pid) throws PwtzDatabaseException
    {
        List<CompanyMenu> list = new ArrayList<CompanyMenu>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("findMenuByPidOrderByRank:|pid=" + pid);
        }
        try
        {
                list = companyMenuMapper.selectMenuByPidOrderByRank(pid);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findMenuByPidOrderByRank:result menu=" + list.size());
        }
        
        return list;
    }
}
